#include "SpotifyLastFmPlugin.h"

void SpotifyLastFmPlugin::OnLoad()
{
  log("spotify_lastfm: Initialized\n");
  m_isSongPlaying = false;
  m_scrobSubmitter.Init(LASTFM_PLUGIN_ID, &SpotifyLastFmPlugin::StatusCallback, (void *)this);
}

void SpotifyLastFmPlugin::OnUnload()
{
  m_scrobSubmitter.Term();
}

void SpotifyLastFmPlugin::OnTrackChanged(SpotifyTrack * newTrack)
{
  if (m_isSongPlaying)
     m_scrobSubmitter.Stop();
  
  std::string artist = std::string(newTrack->artist);
  std::string track = std::string(newTrack->name);
  std::string album = std::string(newTrack->album);
  
  m_scrobSubmitter.Start(artist, track, album, "", newTrack->length, ""); 
  m_isSongPlaying = true;

  // We actually handle the release of incoming track pointer
  //delete newTrack;
}

void SpotifyLastFmPlugin::StatusCallback(int requestId, bool error, std::string message, void * userData)
{
  SpotifyLastFmPlugin * _this = (SpotifyLastFmPlugin *)userData;
  _this->log("spotify_lastfm: Last.fm status: %s\n", message.c_str());
}